﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Ooganizer.Platform.Win32;
using Ooganizer.Resolver;

// Aliases
using ResolverList = System.Collections.Generic.List<Ooganizer.Resolver.IResolver>;

namespace Ooganizer.Resolver
{
    public class ResolverDispatch : IResolver
    {
        public ResolverDispatch()
        {
            m_resolvers = new ResolverList();
            InitializeResolvers();
        }

        private bool InitializeResolvers()
        {
            // Load resolvers

            //
            // Browser Resolvers
            //
            m_resolvers.Add(new InternetExplorerResolver());
            // m_resolvers.Add(new FoxResolver()); --- Not fully implemented

            //
            // Office Resolvers
            //
            m_resolvers.Add(new MSExcelResolver());
            m_resolvers.Add(new MSPowerPointResolver());
            m_resolvers.Add(new MSWordResolver());
            m_resolvers.Add(new MSOneNoteResolver());
            m_resolvers.Add(new MSVisioResolver());
            m_resolvers.Add(new MSAccessResolver());
            m_resolvers.Add(new MSFrontPageResolver());
            m_resolvers.Add(new MSPublisherResolver());

            return true;
        }

        public ArtifactGroup GetArtifacts(IntPtr hWnd)
        {
            string processName = Win32Functions.GetProcessName(hWnd);

            // Find first resolver that handles the process and use it
            foreach (var resolver in m_resolvers)
            {
                IResolveable resolvable = resolver as IResolveable;

                if (resolvable != null && resolvable.CanResolve(processName))
                {
                    return resolver.GetArtifacts(hWnd);
                }
            }

            return new ArtifactGroup();  // no resolver found ... return empty list of artifacts
        }


        private ResolverList m_resolvers;
    }
}
